home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / draggingsprites / src / ds / actions / scaler.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  3.1 KB  |  89 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. package ds.actions;
  9.  
  10. import quicktime.app.actions.*;
  11. import quicktime.qd.*;
  12. import quicktime.*;
  13. import quicktime.std.image.Matrix;
  14. import quicktime.app.image.Transformable;
  15. import quicktime.app.display.Drawable;
  16.  
  17. import java.awt.Dimension;
  18. import java.awt.event.MouseEvent;
  19.  
  20. /**
  21.  * A class that provides the implementation of where a dragged object will be scaled according
  22.  * to the amount of pixels from the origin the user drags the mouse.
  23.  */
  24. public class Scaler extends Dragger {
  25. //____________________________ CLASS METHODS
  26.     /**
  27.      * Set some parameters that will create DragActions. 
  28.      * @param modifierKeyMask - if specified will determine which modifier keys must
  29.      * be depressed for the action to be invoked.
  30.      */
  31.     public Scaler (int scaleFactor, int modifierKeyMask) {
  32.         this (scaleFactor, modifierKeyMask, MouseResponder.kModifiersExactMatch, 0);
  33.     }
  34.  
  35.     /**
  36.      * Set some parameters that will create DragActions. 
  37.      * @param modifierKeyMask - if specified will determine which modifier keys must
  38.      * be depressed for the action to be invoked.
  39.      * @param modifierTestConditions the test conditions under which the modifier mask is tested
  40.      */
  41.     public Scaler (int scaleFactor, int modifierKeyMask, int modifierTestConditions, int addedEventInvoker) {
  42.         super (modifierKeyMask, modifierTestConditions, addedEventInvoker);
  43.         this.scaleFactor = scaleFactor;
  44.     }    
  45.     
  46. //____________________________ INSTANCE VARIABLES
  47.     private int xOrigin, yOrigin;    
  48.     private int scaleFactor;
  49.  
  50. //____________________________ INSTANCE METHODS
  51.     /**
  52.      * This method is used by the DragController when the mouse is first pressed down on 
  53.      * the draggable object. If you wish to do anything to your draggable object before it
  54.      * is dragged then you should overide this method. The default implementation does nothing.
  55.      * @param event the mouse down event that may begin the drag action
  56.      * @param space The drawable object is the space within which the event has occured.
  57.      */
  58.     public void mousePressed (MouseEvent event) {
  59.         xOrigin = event.getX();
  60.         yOrigin = event.getY();
  61.     }
  62.  
  63.     /**
  64.      * This method is called by Dragger when an event is received that meets the conditions for 
  65.      * the object to be dragged.
  66.      * <P>
  67.      * This method will allow the user to alter the size of the object by dragging on it.
  68.      * @param event the mouse drag event that triggered the drag action.
  69.      * @param space The drawable object is the space within which the event has occured.
  70.      */
  71.     public void mouseDragged (MouseEvent event) {
  72.         try {
  73.             Matrix mat = target.getMatrix();
  74.  
  75.             float x = (event.getX() - xOrigin) / (float)scaleFactor;
  76.             float y = (event.getY() - yOrigin) / (float)scaleFactor;
  77.             xOrigin = event.getX();
  78.             yOrigin = event.getY();
  79.             
  80.             mat.setSx (mat.getSx() + x);
  81.             mat.setSy (mat.getSy() + y);
  82.             
  83.             target.setMatrix (mat);
  84.         } catch (QTException e) {
  85.             throw new QTRuntimeException (e);
  86.         }
  87.     }
  88. }
  89.